package org.rednebula.framework.core.model.form;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.rednebula.framework.core.IEnvironment;
import org.rednebula.framework.core.log.Log;
import org.rednebula.framework.core.model.RequestType;
import org.rednebula.framework.core.model.SelfName;
import org.rednebula.framework.core.model.domain.IModel;
import org.rednebula.framework.core.service.IAssembleService;
import org.rednebula.framework.core.service.ICacheService;
import org.rednebula.framework.core.service.ICombineService;
import org.rednebula.framework.core.service.IHandlerService;
import org.rednebula.framework.core.service.ServiceFacade;
import org.rednebula.framework.core.utils.ExtendsStringUtils;
import org.rednebula.framework.core.utils.InternalConstant;



public abstract class BasicForm  extends SelfName implements IForm , Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected static Log logger=Log.getLog(BasicForm.class);
	
	private RequestType requestType;  //当前请求类型
	
	private ICombineService  combineService;

	private IAssembleService assembleService;
	
	IHandlerService handlerService;

	private String version;  //备用
	
	private IModel rootModel;
	
	private Map<Object,Object> temporary=new HashMap<Object,Object>();
	
	private boolean cache=false;
	
	public boolean isCache() {
		return cache;
	}

	public void setCache(boolean cache) {
		this.cache = cache;
	}

	public void setRootModel(IModel rootModel) {
		this.rootModel = rootModel;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public void setRequestType(RequestType requestType) {
		this.requestType = requestType;
	}

	public RequestType getRequestType() {
		return requestType;
	}

	public IModel getRootModel() {
		return rootModel;
	}

	public String getVersion() {
		return version;
	}
	
	public IModel handle(IEnvironment env){
		IModel model=null;
		setAssembleService((IAssembleService)env.lookup(this.getAssembleServiceName()));
		
		setHandlerService((IHandlerService)env.lookup(this.getHandlerServiceName()));
		
		this.setCombineService(ServiceFacade.getCombineService(env));
		
		if(RequestType.create.equals(getRequestType())){
			model= create( env);
		}else if(RequestType.find.equals(this.getRequestType())){
			model= find(env);
		}else if(RequestType.modify.equals(this.getRequestType())){
			modify(env);
		}else if(RequestType.delete.equals(this.getRequestType())){
			delete(env);
		}else {
			model=other(env);
		}
		
		if(this.cache)
			cacheModel( model, env);
		return model;
	}
	

	protected final IModel find(IEnvironment env) {
		
		return getCombineService().find(env, this);
	}
	
    public void putInTemporary(Object key , Object value){
    	getTemporary().put(key, value);
    }
	 
	public  <T> T getTemporayData(Object key){
		return (T)getTemporary().get(key);
	}
	
	public void clearTemporaryData() {
		getTemporary().clear();
		
	}
	protected final IModel create(IEnvironment env) {
		return this.getCombineService().create(env, this);
	}

	protected final void modify(IEnvironment env) {
		getCombineService().modify(env, this);
	}
	
	protected final void delete(IEnvironment env){
		this.getCombineService().delete(env,this);
	}
	
	protected final IModel other(IEnvironment env){
		return getCombineService().other(env, this);
	}
	
	protected Map<Object,Object> getTemporary(){
		return this.temporary;
	}
	
	public ICombineService getCombineService() {
		return combineService;
	}

	public void setCombineService(ICombineService combineService) {
		this.combineService = combineService;
	}

	public IHandlerService getHandlerService() {
		return handlerService;
	}

	public void setHandlerService(IHandlerService handleService) {
		this.handlerService = handleService;
	}

	public IAssembleService getAssembleService() {
		return assembleService;
	}

	public void setAssembleService(IAssembleService assembleService) {
		this.assembleService = assembleService;
	}
	
	public String getSelfSuffix(){
		return InternalConstant.FORM_SUFFIX;
	}
	/**
	 * Form对应的assembleService 名称的规则:  Basic + AssembleService
	 * @return
	 */
	public String getAssembleServiceName(){
		String formName=getSelfName();
		return formName+InternalConstant.ASSEMBLESERVICE_SUFFIX;
	}
	
	/**
	 * Form对应的HandlerService 名称的规则:  Basic + HandlerService
	 * @return
	 */
	public String getHandlerServiceName(){
		String formName=getSelfName();
		return formName+InternalConstant.HANDLERSERVICE_SUFFIX;
	}
	
	
	
	protected ICacheService getCacheService(IEnvironment env){
		return ServiceFacade.getCacheService(env);
	}
	
	public IModel findModelCache(IEnvironment env){
		ICacheService cache=getCacheService(env);
		return cache.getModel(InternalConstant.INTERNAL_MODEL_INSESSION_NAME, env);
	}
	
	public void cacheModel(IModel model, IEnvironment env){
		ICacheService cache=getCacheService(env);
		cache.cache(InternalConstant.INTERNAL_MODEL_INSESSION_NAME, model, env);
	}
}
